package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.UpdateVersion;
import com.example.jbmsuppliers.sinopec.doorjurisd.SensorService;
import com.example.jbmsuppliers.sinopec.net.NetDeceVersion;
import com.example.jbmsuppliers.sinopec.net.busheven.UtlisNetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.jbmsuppliers.sinopec.view.DoubleButtonDialog;
import com.example.permission.myjbm.utils.LoginSharedPreferences;
import com.jbm.jbmsupplier.util.IAppVersion;
import com.jbm.jbmsupplier.util.ICtrl;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by 家帮忙 on 2017/1/17.
 */

public class ActivitySet extends ActivityUse {

    @InjectView(R.id.goback_ps_set)
    Button gobackPsSet;
    @InjectView(R.id.te_setver)
    TextView teSetver;
    @InjectView(R.id.version)
    TextView version;
    @InjectView(R.id.gy)
    TextView gy;
    @InjectView(R.id.exti)
    TextView exti;
    private DialogLoading mDialog;
    private final int DECEVERSION = 0001;
    private final int EXITLOGIN=0002;
    private String versions;
    private LoginSharedPreferences shres;
    private LoginSharedPreferences sp;
    private SharedPreferences mSharedPreferences;

    private SharedPreferences.Editor editshar;
    @Override
    protected int setContentView1() {
        return R.layout.activity_set;
    }

    @Override
    protected void initView() {
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);
        sp=new LoginSharedPreferences(this);
        shres=new LoginSharedPreferences(this);
        mSharedPreferences = getSharedPreferences("sinopecall", Activity.MODE_PRIVATE);
        editshar = mSharedPreferences.edit();
        PackageManager packagema = getPackageManager();
        PackageInfo pack = null;
        try {
            pack = packagema.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        versions = pack.versionName;
        teSetver.setText(versions + "版本");
    }



    @OnClick({R.id.version, R.id.gy, R.id.exti, R.id.goback_ps_set})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.version:
               int mode= UtlisNetWork.getnetwokstate(this);
                if (mode==-1){
                    hint("网络无连接请链接后重试");
                    return;}
                setdecetionVersion();
                break;
            case R.id.gy:
                startActivity(new Intent(this, MyWoDeXiamgQing.class));
                break;
            case R.id.exti:
                UserExit();
                break;
            case R.id.goback_ps_set:
                finish();
                break;

        }
    }

    //检测版本更新
    private void setdecetionVersion() {

        String max = versions.substring(0, versions.indexOf("."));
        String min = versions.substring(versions.indexOf(".") + 1, versions.length());
        NetDeceVersion netdecever = new NetDeceVersion(handler);
        netdecever.setDeceVer(Integer.parseInt(max), Integer.parseInt(min), DECEVERSION);
        mDialog = new DialogLoading(this, "检测中....", true);
        if (mDialog != null)
            mDialog.show();

    }

    /**
     * 用户退出的时候使用
     */

    private void UserExit() {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(this, "你是否要退出当前用户！");
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivitySet.this.deleteFile("openkey.txt");
//                stopService(new Intent(ActivitySet.this, FloatWindowService.class));//停止悬浮按钮
                stopService(new Intent(ActivitySet.this, SensorService.class));//停止摇一摇
                editshar.putInt("result.login_id",0).commit();
                sendBroadcast(new Intent("isBack").putExtra("login",false).putExtra("relogin","login1"));//
                sp.saveMerchantState(false);
                shres.setLoginState(false);
                NetDeceVersion netDeceVersion=new NetDeceVersion(handler);
                netDeceVersion.setExit(EXITLOGIN);
                MainActivity.goNewHome();
                dialog.dismiss();

                finish();
            }
        });
        dialog.getnobtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });


    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DECEVERSION: {
                    final IAppVersion result = (IAppVersion) msg.obj;
                    if (result != null) {
                        if (result.back && !result.app_url.isEmpty()) {
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                            final DoubleButtonDialog dialog = new DoubleButtonDialog(ActivitySet.this, result.errorinfo);
                            dialog.setNoText("下次更新");
                            dialog.setTetle_message("更新新版本");
                            dialog.setisVisibilitymess();
                            dialog.getokbtn().setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    new UpdateVersion(ActivitySet.this, result.app_url);
                                    dialog.dismiss();
                                }
                            });
                            dialog.getnobtn().setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (result.milestone > 0) {
                                        finish();
                                    } else {
                                    }
                                    dialog.dismiss();
                                }
                            });
                        }
                        if (result.equals("time out")) {
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                            hint(result.errorinfo);
                        } else {
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                            hint("已经是最新版本");
                        }
                    }
                    break;
                }
                case EXITLOGIN:{
                    ICtrl exit=(ICtrl)msg.obj;
                    if (exit.back){

                    }else{

                    }


                }
            }
            super.handleMessage(msg);
        }
    };



}
